﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Hosting;
using System.Web;
using System.IO;
using System.Reflection;

namespace MethodWorx.CMS.Helpers
{
    public class AssemblyResourceVirtualFile : VirtualFile
    {
        string path; 
        public AssemblyResourceVirtualFile(string virtualPath) : base(virtualPath) 
        {
            path = virtualPath;
        }
        public override System.IO.Stream Open()
        {

            string []parts = path.Split('/');
        
            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                try
                {
                    if (assembly.GetName().Name.ToLower() == parts[parts.Length - 2].ToLower())
                    {
                        return assembly.GetManifestResourceStream(parts[parts.Length - 1]);
                    }
                }
                catch (Exception)
                {
                    
                    //throw;
                }
                
            } 
            return null; 
        } 
    }
}

